﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <vegas@ekameleon.net>.
  Portions created by the Initial Developer are Copyright (C) 2004-2007
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
 */
import andromeda.model.map.MapModel;

import egallery.vo.PictureVO;

import vegas.data.iterator.Iterator;

/**
 * The picture model of the application.
 * @author eKameleon
 */
class egallery.model.PictureModel extends MapModel 
{
	
	/**
	 * Creates a new PictureModel instance. 
	 * @param id the id of this model.
	 * @param bGlobal the flag to use a global event flow or a local event flow.
	 * @param sChannel (optional) the name of the global event flow if the {@code bGlobal} argument is {@code true}.
	 */
	public function PictureModel(id, bGlobal:Boolean, sChannel:String)
	{
		
		super( id, bGlobal, sChannel );
		
		buRRRn.eden.Application.addAuthorized( "egallery.vo.PictureVO" ) ;
		
	}
	
	/**
	 * Clear the model.
	 */
	public /*override*/ function clear():Void
	{
		_it = null ;
		super.clear() ;
	}
	
	/**
	 * Sets the next PictureVO in the model.
	 */
	public function next():Void
	{
		if ( ! _it.hasNext() )
		{
			_it.reset() ;
		}
		setCurrentVO( getVO(_it.next())) ;
		_currentKey = _it.key() ;
	}

	/**
	 * Sets the previous PictureVO in the model.
	 */
	public function prev():Void
	{
		_currentKey -- ;
		if (_currentKey < 0)
		{
			_currentKey = size() - 1 ;
		}
		_it.seek(  _currentKey  ) ;
		setCurrentVO( getVO(_it.next())) ;
	}
	
	/**
	 * Reset the iterator of the Gallery.
	 */
	public function reset():Void
	{
		if (size() > 0)
		{
			_it = getMap().keyIterator() ;
		}
		else
		{
			_it = null ;
		}	
	}
	
	/**
	 * Run the model to launch the next picture in the Picture display.
	 */
	public /*override*/ function run():Void
	{
		reset() ;
		next() ;		
	}

    /**
     * Returns {@code true} if the {@code IValidator} object validate the value. Overrides this method in your concrete IModelObject class.
     * @param value the object to test.
     * @return {@code true} is this specific value is valid.
     */
     public /*override*/ function supports( value ):Boolean 
     {
         return value instanceof PictureVO ;
     }

	/**
	 * The current key of the iterator.
	 */
	private var _currentKey:Number ;
	
	/**
	 * The internal iterator to show the pictures.
	 */
	private var _it:Iterator ;

}
